package com.example.androidcoursedesing.main;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;


import com.example.androidcoursedesing.R;
import com.example.androidcoursedesing.main.adapter.MyFragmentPagerAdapter;
import com.example.androidcoursedesing.main.tab.Fragment1;
import com.example.androidcoursedesing.main.tab.Fragment2;
import com.example.androidcoursedesing.main.tab.Fragment3;
import com.example.androidcoursedesing.main.tab.Fragment4;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
   private ViewPager2 mViewPager;
   private  TabLayout mTabLayout;
   private Context mContext;

   private List<Fragment> fragmentLists;
   private String[] tabTitles= new String[]{"推荐","热门","纪录片","课堂"};
   public void setContext(Context context){
       this.mContext=context;
   }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment_layout, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
       //初始化view
        initView(view);
        //初始化fragment
        initFragmentList();
    }

    private void initView(View view) {
         mTabLayout = view.findViewById(R.id.mTabLayout);
         mViewPager = view.findViewById(R.id.mViewPager2);
    }

    private void initFragmentList() {
        fragmentLists= new ArrayList<>();
        Fragment1 f1 = new Fragment1();
        f1.setContext(mContext);
        Fragment2 f2 = new Fragment2();
        Fragment3 f3 = new Fragment3();
        Fragment4 f4 = new Fragment4();
        fragmentLists.add(f1);
        fragmentLists.add(f2);
        fragmentLists.add(f3);
        fragmentLists.add(f4);
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter((FragmentActivity) mContext, fragmentLists);
//        mViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
//            @Override
//            public void onPageSelected(int position) {
//
//            }
//        });
        //设置适配器
        mViewPager.setAdapter(adapter);
        //这里必须要在添加适配器完了之后才能新建该方法
        // Mediator[ˈmiːdieɪtər]斡旋者
        new TabLayoutMediator(mTabLayout, mViewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //就是将标题内容分别相对应的赋值
                tab.setText(tabTitles[position]);
            }
        }).attach();//最后一定要.attach()
    }

}
